﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Chart.Component;
using System.Drawing;

namespace AAA.Chart.Style
{
    public class CandleStickChart : DefaultDrawMethod
    {
        public override void Draw(System.Drawing.Graphics g)
        {
            Pen posPen = new Pen(new SolidBrush(Color.Red), Width);
            Pen negPen = new Pen(new SolidBrush(Color.Green), Width);
            Pen equalPen = new Pen(new SolidBrush(ForeColor), Width);
            Pen shadowPen = new Pen(new SolidBrush(ForeColor), 1);
            int[] iStart;
            int[] iEnd;
            
            for (int i = StartIndex; i < EndIndex; i++)
            {
                try
                {
                    iStart = ChartMeta.ValueToPixel(i,
                                                    High[i]);
                    iEnd = ChartMeta.ValueToPixel(i,
                                                  Low[i]);

                    if (IsValidate(iStart) && IsValidate(iEnd))
                        g.DrawLine(shadowPen, new Point(iStart[0], iStart[1]), new Point(iEnd[0], iEnd[1]));

                    iStart = ChartMeta.ValueToPixel(i,
                                                    Open[i]);
                    iEnd = ChartMeta.ValueToPixel(i, Close[i]);

                    if (IsValidate(iStart) && IsValidate(iEnd))
                        g.DrawLine(Open[i] < Close[i] 
                                    ? posPen 
                                    : Open[i] > Close[i]
                                        ?   negPen
                                        :   equalPen, 
                                        new Point(iStart[0], Open[i] == Close[i] ? iStart[1] - 1 : iStart[1]), 
                                   new Point(iEnd[0], iEnd[1]));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + "," + ex.StackTrace);
                }
            }

        }
    }
}
